package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Cart;
import com.example.demo.service.CartService;
import com.example.demo.mapper.CartMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GJC
 * @description 针对表【cart】的数据库操作Service实现
 * @createDate 2025-01-12 23:14:37
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart>
        implements CartService {
    //根据图书ID将图书添加到购物车中。
    public int addCart(Cart cart) {
        return this.baseMapper.insert(cart);
    }

    //获取购物车中的图书接口，返回购物车中的图书列表及其数量
    public List<Cart> getList(int user_id) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user_id);
        return this.baseMapper.selectList(queryWrapper);
    }

    //根据图书ID更新购物车中图书的数量。
    public int updateQuantity(Cart cart) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", cart.getUserId());
        queryWrapper.eq("book_id", cart.getBookId());
        return this.baseMapper.update(cart,queryWrapper);
    }

    // 删除指定用户的购物车中的所有记录
    public int clearCart(int user_id) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user_id);
        return this.baseMapper.delete(queryWrapper);
    }
}




